package com.njcc.service;

import java.io.FileInputStream;
import java.io.IOException;

import org.apache.poi.POIXMLDocument;
import org.apache.poi.hwpf.extractor.WordExtractor;
import org.apache.poi.xwpf.usermodel.XWPFDocument;

import com.itextpdf.text.pdf.PdfReader;
import com.njcc.util.DBCon;

public class GetFilePages {

	private String file_Path = DBCon.file_Path;
	/**
	 * 读取指定位置的后缀名为doc的word文件，解析获得文件页数
	 * @param name 文件名
	 * @return 返回Word文件页数
	 * @throws IOException IOException
	 */
	public int getPages4DOC(String name) throws IOException{
		String filePath = file_Path+name;
		WordExtractor doc = new WordExtractor(new FileInputStream(filePath));
		int pageCount = doc.getSummaryInformation().getPageCount();
		doc.close();
		return pageCount;
	}
	
	/**
	 * 读取指定位置的后缀名为docx的word文件，解析获得文件页数
	 * @param name 文件名
	 * @return 返回word页数
	 * @throws IOException IOException
	 */
	public int getPages4DOCX(String name) throws IOException{
		String filePath = file_Path+name;
		XWPFDocument docx = new XWPFDocument(POIXMLDocument.openPackage(filePath)); 
		int pages = docx.getProperties().getExtendedProperties().getUnderlyingProperties().getPages();//总页数 
		docx.close();
		System.out.println("docx:"+pages);
		return pages;
	}
	
	/**
	 * 读取指定位置的PDF文件，解析获得页数
	 * @param name 文件名
	 * @return PDF文件页数
	 * @throws IOException IOException
	 */
	public int getPDFPages(String name) throws IOException{
		String filePath = file_Path+name;
		PdfReader p = new PdfReader(filePath);
		int numberOfPages = p.getNumberOfPages();
		p.close();
		return numberOfPages;
	}

}
